package com.gilight.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.common.Constant;
import com.gilight.base.dto.ColdCategoryDto;
import com.gilight.base.dto.ColdProductDto;
import com.gilight.base.dto.ColdProductQueryDto;
import com.gilight.base.model.ColdCategory;
import com.gilight.base.dao.ColdCategoryMapper;
import com.gilight.base.service.IColdCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gilight.base.service.IColdProductService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shr
 * @since 2021-07-15
 */
@Service
public class ColdCategoryServiceImpl extends ServiceImpl<ColdCategoryMapper, ColdCategory> implements IColdCategoryService {

    @Autowired
    private ColdCategoryMapper coldCategoryMapper;
    @Autowired
    private IColdProductService coldProductServiceImpl;

    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST,allEntries=true)
    @Override
    public void addProductList(ColdCategoryDto coldCategoryDto) {
        //先移除，防止重复添加
        this.removeProductList(coldCategoryDto);
        coldCategoryMapper.addProductList(coldCategoryDto);
    }

    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST,allEntries=true)
    @Override
    public void removeProductList(ColdCategoryDto coldCategoryDto) {
        List<ColdProductDto> list = coldCategoryDto.getColdProductDto();
        if(CollectionUtils.isNotEmpty(list)){
            for (ColdProductDto coldProductDto:list) {
                coldCategoryMapper.removeProduct(coldCategoryDto.getCategoryId(),coldProductDto.getProductId());
            }
        }
    }

    @Cacheable(value = Constant.PORDUCT_CACHE_LIST,key="#zzjgId==null?'':#zzjgId")
    @Override
    public List<ColdCategoryDto> getProductList(Long zzjgId) {
        List<ColdCategoryDto> coldCategoryDtoList = coldCategoryMapper.getCategoryList(zzjgId);
        for (ColdCategoryDto coldCategoryDto: coldCategoryDtoList) {
            ColdProductQueryDto coldProductQueryDto = new ColdProductQueryDto();
            coldProductQueryDto.setCategoryId(coldCategoryDto.getCategoryId());
            coldProductQueryDto.setZzjgId(zzjgId);
            coldProductQueryDto.setStatus(1);
            List<ColdProductDto> productList = coldProductServiceImpl.getProductList(coldProductQueryDto);
            coldCategoryDto.setColdProductDto(productList);
        }
        return coldCategoryDtoList;
    }

    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST,allEntries=true)
    @Override
    public boolean save(ColdCategory entity) {
        return super.save(entity);
    }

    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST,allEntries=true)
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST,allEntries=true)
    @Override
    public boolean updateById(ColdCategory entity) {
        return super.updateById(entity);
    }

    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST,allEntries=true)
    @Override
    public boolean saveOrUpdate(ColdCategory entity) {
        return super.saveOrUpdate(entity);
    }
}
